package edu.uit.se.slgroup.construct.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.uit.se.slgroup.construct.domain.User;
import edu.uit.se.slgroup.construct.services.UserService;

@Controller
public class UserController {

	@Autowired
	UserService userService;
	
	@RequestMapping("register-form")
	public ModelAndView getForm(@ModelAttribute User user) {
		System.out.println("get form");
		return new ModelAndView("register-user");	//register-user.jsp
	}
	
	@RequestMapping("registerUser")
	public ModelAndView registerUser(@ModelAttribute User user) {
		userService.insertUser(user);
		return new ModelAndView("redirect:user-list-form");
	}
	
	@RequestMapping("user-list-form")
	public ModelAndView getUserList() {
		List<User> userList = userService.getUserList();
		return new ModelAndView("user-list", "userList", userList);	//user-list.jsp
	}
	
	@RequestMapping("delete")
	public ModelAndView deleteUser(@RequestParam int id) {
		userService.deleteUser(id);
		return new ModelAndView("redirect:user-list-form");
	}
	
	@RequestMapping("edit-user-form")
	public ModelAndView editUser(@RequestParam int id, @ModelAttribute User user) {
		User userObj = userService.getUserById(id);		
		return new ModelAndView("edit-user", "userObject", userObj);	//edit-user.jsp
	}
	
	@RequestMapping("update")
	public ModelAndView updateUser(@ModelAttribute User user) {
		userService.updateUser(user);
		return new ModelAndView("redirect:user-list-form");
	}
}
